<?php

namespace app\api\controller;

use app\admin\model\scenic\ScenicCategory;
use app\api\model\scenic\ScenicEvaluate as ScenicEvaluateModel;
use app\api\model\scenic\ScenicTickets;
use app\common\controller\Api;

use app\api\model\scenic\ScenicZan as ScenicZanModel;
use app\api\model\scenic\Scenic as ScenicModel;

/**
 * 景区点赞接口
 */
class Sceniczan extends Api
{

    protected $noNeedRight = ['*'];

    //点赞
    public function add()
    {
        $scenic_id = input('scenic_id',0);
        if (empty($scenic_id)) {
            $this->error('缺少参数景区id');
        }
        $data = [
            'user_id' => $this->auth->id,
            'scenic_id' => $scenic_id,
        ];

        $collect = new ScenicZanModel();

        $info = ScenicZanModel::where(['scenic_id'=>$scenic_id, 'user_id' => $this->auth->id])->find();
        if (empty($info)){
            $res = $collect->save($data);
            if ($res){
                ScenicModel::where(['scenic_id'=>$scenic_id])->setInc('zan_num',1);
            }
            $this->success('点赞成功',$res);
        } else {
            $this->error('点赞失败');
        }
    }

    //删除
    public function delete()
    {
        $scenic_id = input('scenic_id',0);
        if (empty($scenic_id)) {
            $this->error('缺少参数景区id');
        }

        $info = ScenicZanModel::where(['scenic_id'=>$scenic_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)){
            $res = $info->delete();
            if ($res){
                ScenicModel::where(['scenic_id'=>$scenic_id])->setDec('zan_num',1);
            }
            $this->success('取消成功',$res);
        } else {
            $this->error('取消失败');
        }
    }

    //是否点赞
    public function iszan()
    {
        $scenic_id = input('scenic_id',0);
        if (empty($scenic_id)) {
            $this->error('缺少参数景区id');
        }

        $info = ScenicZanModel::where(['scenic_id'=>$scenic_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)) {
            $this->success('',1);
        } else {
            $this->success('',0);
        }
    }

    //收藏列表
    public function lists()
    {
        $lat = input('lat','');
        $lng = input('lng','');

        $list = ScenicZanModel::alias('pz')
            ->join('scenic s','pz.scenic_id = s.scenic_id')
            ->where(['pz.user_id'=>$this->auth->id])
            ->field('pz.createtime,s.scenic_id,s.name,s.level,s.intro,s.category_id,s.zan_num,s.lat,s.lng')
            ->order('pz.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $model = new ScenicModel();
        $level_list = $model->getLevelList();
        foreach ($list as $k => $item) {
            if ($item[ 'lng' ] && $item[ 'lat' ]&&$lng&&$lat) {
                $distance = getDistance((float) $item[ 'lng' ], (float) $item[ 'lat' ], (float) $lng, (float) $lat);
                $list[ $k ][ 'distance' ] = round($distance / 1000,1);
            } else {
                $list[ $k ][ 'distance' ] = 0;
            }
            $list[ $k ][ 'comment_num' ] =  ScenicEvaluateModel::where(['scenic_id'=>$item['scenic_id']])->count();

            $tickets_list = ScenicTickets::where(['scenic_id'=>$item['scenic_id']])->column('price');
            $list[ $k ][ 'start_price' ] = count($tickets_list)>0?min($tickets_list):0;
            $category_list = ScenicCategory::where('id','in',$item['category_id'])->column('name');
            $list[ $k ][ 'category_id_text' ] = $category_list;
            $list[ $k ][ 'level_text' ] = $level_list[$item['level']];
        }
        $this->success('',$list);
    }

    //景区点赞排名
    public function ranking()
    {
        $lat = input('lat','');
        $lng = input('lng','');

        $list = ScenicModel::field('scenic_id,name,level,intro,category_id,zan_num,lat,lng')
            ->order('zan_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        foreach ($list as $k => $item) {
            if ($item[ 'lng' ] && $item[ 'lat' ]&&$lng&&$lat) {
                $distance = getDistance((float) $item[ 'lng' ], (float) $item[ 'lat' ], (float) $lng, (float) $lat);
                $list[ $k ][ 'distance' ] = round($distance / 1000,1);
            } else {
                $list[ $k ][ 'distance' ] = 0;
            }
            $list[ $k ][ 'comment_num' ] =  ScenicEvaluateModel::where(['scenic_id'=>$item['scenic_id']])->count();

            $tickets_list = ScenicTickets::where(['scenic_id'=>$item['scenic_id']])->column('price');

            $list[ $k ][ 'start_price' ] = count($tickets_list)>0?min($tickets_list):0;

        }
        $this->success('',$list);
    }

    //景区点赞列表
    public function zanList()
    {
        $scenic_id = input('scenic_id',0);

        $list = ScenicZanModel::alias('hc')
            ->join('user u','hc.user_id = u.id')
            ->where(['hc.user_id'=>$this->auth->id,'hc.scenic_id'=>$scenic_id])
            ->field('u.nickname,u.avatar,hc.createtime')
            ->order('hc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }
}